package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.bean.Category;
import com.itheima.reggie.bean.Setmeal;
import com.itheima.reggie.bean.SetmealDish;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by IntelliJ IDEA.
 * User: 许志民
 * Date: 2021-11-24
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    @Override
    public void addSetmeal(SetmealDto setmealDto) {
        if(setmealDto == null){
            return;
        }
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public Page<SetmealDto> getPage(Integer page, Integer pageSize, String name) {
        Page<Setmeal> iPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        if(name != null && name != ""){
            wrapper.eq(Setmeal::getName,name);
        }
        Page<Setmeal> page1 = this.page(iPage, wrapper);
        List<Setmeal> records = page1.getRecords();
        List<SetmealDto> list = new ArrayList<>();
        for (Setmeal record : records) {
            SetmealDto st = new SetmealDto();
            BeanUtils.copyProperties(record,st);
            Long categoryId = record.getCategoryId();
            Category category = categoryService.getById(categoryId);
            st.setCategoryName(category.getName());
            list.add(st);
        }
        Page<SetmealDto> spage = new Page<>();
        spage.setRecords(list);
        spage.setTotal(page1.getTotal());
        return spage;
    }

    @Override
    public boolean deleteIds(Long[] ids) {
        UpdateWrapper<Setmeal> wrapper = new UpdateWrapper<>();
        Arrays.asList(ids);
        wrapper.in("id",Arrays.asList(ids)).set("is_deleted",1);
        update(wrapper);

        UpdateWrapper<SetmealDish> setmealDishWrapper = new UpdateWrapper<>();
        setmealDishWrapper.in("setmeal_id",Arrays.asList(ids)).set("is_deleted",1);
        setmealDishService.update(setmealDishWrapper);

        return true;
    }

    /**
     * 批量修改状态
     * @param status
     * @param ids
     * @return
     */
    @Override
    public boolean updateStatus(Integer status, Long[] ids) {
        if(status == null || ids == null){
            return false;
        }
        UpdateWrapper<Setmeal> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id",Arrays.asList(ids)).set("status",status);
        boolean update = this.update(updateWrapper);

        return update;
    }

    @Override
    public SetmealDto getSetmealDto(Long id) {
        if(id == null){
            return null;
        }
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getId,id);
        Setmeal setmeal = getOne(wrapper);
        LambdaQueryWrapper<SetmealDish> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> list = setmealDishService.list(setmealWrapper);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    @Override
    @Transactional
    public boolean updateSetmeal(SetmealDto setmealDto) {
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto,setmeal);
        boolean b = this.updateById(setmeal);
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        List<SetmealDish> list = setmealDishService.list(wrapper);
        setmealDishes = setmealDishes.stream().map(item -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        if(list != null){
            //如果查询出集合不为空则直接删除
            setmealDishService.remove(wrapper);
            //删除之后再将新的数据添加进去
            boolean batch = setmealDishService.saveBatch(setmealDishes);
            return batch;
        }
        //如果没有查询到则直接添加
        boolean saveBatch = setmealDishService.saveBatch(setmealDishes);
        return saveBatch;
    }

    @Override
    public List<Setmeal> findList(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> wapper = new LambdaQueryWrapper<>();
        wapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        wapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        List<Setmeal> list = this.list(wapper);
        if(list == null){
            return null;
        }
        return list;
    }

    @Override
    public SetmealDto setmealDish(Long id) {
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(wrapper);
        Setmeal setmeal = getById(id);
        Category category = categoryService.getById(setmeal.getCategoryId());
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(list);
        setmealDto.setCategoryName(category.getName());
        return setmealDto;
    }
}
